package com.xingfudazi.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xingfudazi.app.domain.*;
import com.xingfudazi.app.enums.ApplyStatusEnum;
import com.xingfudazi.app.enums.MemberTypeEnum;
import com.xingfudazi.app.enums.MessageEnum;
import com.xingfudazi.app.mapper.AppRoleApplyMapper;
import com.xingfudazi.app.service.*;
import com.xingfudazi.common.exception.ServiceException;
import com.xingfudazi.common.utils.DateUtils;
import com.xingfudazi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * @author YuJiaMing
 * date 2024年06月22日 22:11
 */
@Service
public class AppRoleApplyServiceImpl implements IAppRoleApplyService {

    @Autowired
    private IAppMemberService appMemberService;

    @Autowired
    private AppRoleApplyMapper appRoleApplyMapper;

    @Autowired
    private IAppMemberInfoService appMemberInfoService;

    @Autowired
    private IAppAccountService appAccountService;


    @Autowired
    private IAppMessageService appMessageService;


    @Override
    public void add(AppRoleApply appRoleApply) {
        String type = appRoleApply.getType();
        Long userId = SecurityUtils.getUserId();

        // 判断是否已经注册过
        Long aid = appMemberService.selectAppMemberById(userId).getAid();
        AppMember appMember = appMemberService.selectAppMember(aid, MemberTypeEnum.valueOf(type));
        if (appMember != null) {
            throw new ServiceException("已注册请切换账号");
        }

        // 判断是否已经发起过申请
        LambdaQueryWrapper<AppRoleApply> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppRoleApply::getNid, userId);
        wrapper.eq(AppRoleApply::getType, type);
        wrapper.eq(AppRoleApply::getStatus, ApplyStatusEnum.RUNNING.name());
        if (this.appRoleApplyMapper.selectCount(wrapper) > 0) {
            throw new ServiceException("审批中请勿重复提交");
        }

        // 获取手机号昵称等信息
        AppMemberInfo appMemberInfo = appMemberInfoService.selectAppMemberInfoById(userId);
        appRoleApply.setNickName(appMemberInfo.getNickName());
        appRoleApply.setRelName(appMemberInfo.getRelName());
        appRoleApply.setPhone(appMemberInfo.getPhone());
        appRoleApply.setStatus(ApplyStatusEnum.RUNNING.name());
        appRoleApply.setNid(userId);

        // 添加
        this.appRoleApplyMapper.insert(appRoleApply);
    }

    @Override
    public List<AppRoleApply> list(AppRoleApply appRoleApply) {
        LambdaQueryWrapper<AppRoleApply> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(AppRoleApply::getCreateTime);
        return appRoleApplyMapper.selectList(wrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void process(Long id, ApplyStatusEnum status) {
        if (status != ApplyStatusEnum.PASS && status != ApplyStatusEnum.RETURN) {
            throw new IllegalArgumentException();
        }

        // 审核
        AppRoleApply appRoleApply = new AppRoleApply();
        appRoleApply.setId(id);
        appRoleApply.setStatus(status.name());
        appRoleApply.setDealTime(LocalDateTime.now());
        this.appRoleApplyMapper.updateById(appRoleApply);

        // 获取最新对象
        appRoleApply = this.appRoleApplyMapper.selectById(id);
        String type = appRoleApply.getType();

        // 审核通过，发送message、新建app_member账号
        if (status == ApplyStatusEnum.PASS) {
            AppMember appMember = appMemberService.selectAppMemberById(appRoleApply.getNid());

            AppAccount appAccount = appAccountService.selectAppAccountById(appMember.getAid());
            appAccount.setMemberType(type);
            appMemberService.init(appAccount);
        }

        // 发送消息
        AppMessage appMessage = new AppMessage();
        appMessage.setType(MessageEnum.SYSTEM.name());
        appMessage.setCreateTime(LocalDateTime.now());
        appMessage.setRel(appRoleApply.getId().toString());
        appMessage.setSender("系统");
        appMessage.setReceiverId(appRoleApply.getNid());
        appMessage.setTitle("审批通知");
        appMessage.setContent("您的" + (MemberTypeEnum.valueOf(type) == MemberTypeEnum.MATCHMAKER ? "红娘" : "教练") + "审核" +
                (status == ApplyStatusEnum.PASS ? "通过" : "未通过"));
        appMessageService.add(appMessage);
    }




    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public AppRoleApply selectAppRoleApplyById(Long id)
    {
        return appRoleApplyMapper.selectById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param appRoleApply 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<AppRoleApply> selectAppRoleApplyList(AppRoleApply appRoleApply)
    {
        return appRoleApplyMapper.selectList(null);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param appRoleApply 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateAppRoleApply(AppRoleApply appRoleApply)
    {
        return appRoleApplyMapper.updateById(appRoleApply);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteAppRoleApplyByIds(Long[] ids)
    {
        return appRoleApplyMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteAppRoleApplyById(Long id)
    {
        return appRoleApplyMapper.deleteById(id);
    }
}
